package com.lap.auth.adapter.driving.persistence.api.repository;

import java.util.List;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import com.lap.auth.adapter.driving.persistence.api.ApiCache;
import com.lap.auth.adapter.driving.persistence.api.ApiConverter;
import com.lap.auth.adapter.driving.persistence.api.mapper.ApiMapper;
import com.lap.auth.adapter.driving.persistence.api.po.ApiPo;
import com.lap.auth.domain.api.Api;
import com.lap.auth.domain.api.repository.ApiRepository;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Repository
public class ApiRepositoryImpl implements ApiRepository {

  private final ApiConverter converter;
  private final ApiMapper apiMapper;

  @CacheEvict(value = ApiCache.API, allEntries = true)
  @Override
  public void saveAll(List<Api> list) {
    List<ApiPo> doList = converter.toPoList(list);
    apiMapper.insertBatch(doList);
  }

  @CacheEvict(value = ApiCache.API, allEntries = true)
  @Override
  public void deleteByModule(String module) {
    apiMapper.deleteByModule(module);
  }

  @Cacheable(value = {ApiCache.API})
  @Override
  public List<Api> findList() {
    List<ApiPo> list = apiMapper.selectList();
    return converter.toDomains(list);
  }

  @Cacheable(
      value = ApiCache.API,
      key = "T(com.lap.auth.domain.shared.tools.CacheKeyUtil).generateKey(#roleIds)")
  @Override
  public List<Api> findInsideByRoleIds(List<Integer> roleIds) {
    List<ApiPo> list = apiMapper.selectInsideByRoleIds(roleIds);
    return converter.toDomains(list);
  }
}
